﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using CommonUtility;
using BLL;
using Model;

namespace WebApplication
{
    public partial class UserInf : BasePage
    {
        const string FunID = "FA01";
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!IsPostBack)
            {
                bllPermission Perm = new bllPermission(oUser.UserID, FunID);
                hdPermission.Value = new JavaScriptSerializer().Serialize(Perm);
                hdRole.Value = bllUserRole.GetUserRoleList(oUser);

            }

            switch (Common.ToString(Request["Action"]))
            {
                case "GetData":
                    Response.Write(GetData());
                    Response.End();
                    break;
                case "Save":
                    Response.Write(DataSave());
                    Response.End();
                    break;
                case "Process":
                    Response.Write(Process());
                    Response.End();
                    break;
                case "SetRole":
                    Response.Write(SetRole());
                    Response.End();
                    break;
            }


        }

        private string GetData()
        {
            string result = "";
            string key = Common.ConvertSql(Common.ToString(Request["where"]));
            try
            {
                IList<UserInfo> list = bllUser.GetAllUser(key);

                string json = new JavaScriptSerializer().Serialize(list.OrderByDescending(d => d.RID).ToList());
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("UsersInf.aspx.cs/LoadData(" + key + ") " + oUser.RID, ex);
            }
            return result;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        private string DataSave()
        {
            string res = string.Empty;
            UserInfo user = new UserInfo();
            user.RID = Common.ToInt32(Request["RID"]);
            user.UserID = Common.ToString(Request["UserID"]);
            user.Name = Common.ToString(Request["Name"]);
            user.Email = Common.ToString(Request["Email"]);
            user.Pwd = Common.ToString(Request["Pwd"]);
            user.EightID = Common.ToString(Request["EightID"]);
            user.Flag = Common.ToString(Request["Flag"]);
            user.RoleList = Common.ToString(Request["RoleList"]);

            res = bllUser.SaveUser(FunID, user,"1", oUser);
            
            return res;
        }

        /// <summary>
        /// Active
        /// </summary>
        /// <returns></returns>
        private string Process()
        {
            string res = string.Empty;
            string type = Common.ToString(Request["type"]);
            string rids = Common.ConvertSql(Common.ToString(Request["rids"]));
            if (string.IsNullOrEmpty(rids)) return "No parameters";

            res = bllUser.ProcessUser(FunID, rids, type, oUser);

            return res;
        }

        private string SetRole()
        {
            string result = string.Empty;
            string rids = Common.ToString(Request["rids"]);
            if (string.IsNullOrEmpty(rids)) return "No parameters";
            string[] rID = rids.Split(',');
            for (int i = 0; i < rID.Length; i++)
            {
                string userID = Convert.ToString(rID[i].Split('*')[0]);
                string roleList = Convert.ToString(rID[i].Split('*')[1]);
                result =bllUserRole.SetRole(FunID, userID, roleList, oUser);
            }
            return result;
        }
    }
}